﻿Imports System.Threading
Imports BKO.Core
Imports BKO.Domain.Model
Imports BKO.UIControls

Module MOD_REF_TREE
	Private lbl_M_F As String
	Private lbl_M_F1 As String
	Private lbl_M_F2 As String
	Private filialName, otdelName, cabinetName As String
	Private lstgroups1 As TreeView
	Private BrancheNode1 As TreeNode
	Private SelectedNode As TreeNode
	Private FontStyl As System.Drawing.FontStyle
	Private iA1, iA2, iA3, iA4, iA5, iA6, iA7, iA8, iID As String

	Private Sub FILING_FILIAL(ByVal comps As IEnumerable(Of IComp))
		On Error Resume Next

		If Len(filialName) = 0 Then Exit Sub

		Dim sVISIBLE As String
		Dim objIniFile As New IniFile(PrPath & "base.ini")

		Dim sText As String = objIniFile.GetString("general", "Tree_S", 0)

		Dim comps1 As IEnumerable(Of IComp)

		Select Case sText
			Case 0
				comps1 = comps.OrderBy(Function(c) c.PSEVDONIM).OrderBy(Function(c) c.TipTehn)
			Case 1
				comps1 = comps.OrderBy(Function(c) c.TipTehn).OrderBy(Function(c) c.PSEVDONIM)
		End Select

		For Each comp As IComp In comps1
			iA1 = comp.TIP_COMPA
			iA2 = comp.NET_NAME
			iA3 = comp.PSEVDONIM
			iA4 = comp.Spisan
			iA5 = comp.TipTehn
			iA6 = comp.OS
			iA7 = comp.PRINTER_NAME_4
			iA8 = comp.Balans
			iID = comp.ID

			FILING_TREE(lstgroups1, iA5, iA1, iA2, iA3, iID, iA4, BrancheNode1, iA6, iA7, iA8)
		Next
	End Sub

	Public Sub RefFilTree(ByVal lstgroups As TreeView)
		'On Error GoTo ERR1

		'Dim starttime As DateTime
		'starttime = Now()

		'lstgroups.visible = False
		lstgroups.HideSelection = False
		lstgroups.ShowRootLines = False

		Dim sTEN As String
		Dim sVISIBLE As String

		Dim objIniFile As New IniFile(PrPath & "base.ini")
		sVISIBLE = objIniFile.GetString("general", "VisibleALL", "")
		KCKey = objIniFile.GetString("general", "DK", 0)
		DCKey = objIniFile.GetString("general", "Default", 0)

		Dim zBranche As String
		zBranche = objIniFile.GetString("general", "branche", "")

		If Len(zBranche) = 0 Or zBranche = frmMain.lang.GetString("frmComputers", "MSG53", "") Then
			zBranche = Nothing
		End If

		lstgroups.Nodes.Clear()

		Select Case sICONS
			Case "32*32"
				sICONS = sICONS
			Case Else
				sICONS = "24*24"
		End Select

		If frmComputers.ilsCommands.Images.Count = 0 Then
			frmComputers.ilsCommands.LoadIcons(PrPath, Constants.TreeIconPath, sICONS, "sCMP")
		End If

		lstgroups.ImageList = frmComputers.ilsCommands

		'Компы
		Dim comps As List(Of IComp) = frmMain.compService.GetAll().Where(Function(c) c.PCL = 0).ToList()

		'Верхний нулевой уровень - вставляем название организации
		Dim nodeRoot As New TreeNode(frmMain.cfg.ORG, 69, 69)
		nodeRoot.Tag = "ROOT" & GENID()

		'Филиалы Первый уровень дерева
		'Проверяем все показывать или только активные
		Dim filials As List(Of IFilial) = frmMain.filialService.GetAllByBranch(zBranche, sVISIBLE = 1)

		lstgroups1 = lstgroups

		For Each filial As IFilial In filials
			filialName = filial.Name
			otdelName = String.Empty
			cabinetName = String.Empty

			Dim BrancheNode As New TreeNode(filialName, 0, 0)
			BrancheNode.Tag = "G|" & filial.ID
			sTEN = "G|" & filial.ID
			nodeRoot.Nodes.Add(BrancheNode)

			BrancheNode1 = BrancheNode

			If KCKey = 0 And Len(DCKey) <> 0 Then
				If DCKey = sTEN Then
					SelectedNode = BrancheNode
					SelectedNode.Expand()
				End If
			End If

			BrancheNode.ForeColor = Color.DarkBlue

			'Вставляем технику если есть
			Dim filialComps As IEnumerable(Of IComp) = comps _
				.Where(Function(c) c.FILIAL = filialName And c.MESTO = String.Empty)
			Dim cFil As Integer = filialComps.Count()

			If cFil <> 0 Then
				Call FILING_FILIAL(filialComps)
			End If
			filialComps = Nothing

			'Отделы в филиалах Второй уровень дерева
			Dim otdels As List(Of IOtdel) = frmMain.otdelService.GetAllByFilial(filial.ID, sVISIBLE = 1)

			For Each otdel As IOtdel In otdels
				If otdel.FilialId = filial.ID Then
					otdelName = otdel.Name
					cabinetName = String.Empty

					Dim DepatrmentNode As New TreeNode(otdelName, 1, 1)
					DepatrmentNode.Tag = "O|" & otdel.ID
					sTEN = "O|" & otdel.ID
					BrancheNode.Nodes.Add(DepatrmentNode)

					BrancheNode1 = DepatrmentNode

					If KCKey = 0 And Len(DCKey) <> 0 Then
						If DCKey = sTEN Then
							SelectedNode = DepatrmentNode
							SelectedNode.Expand()
						End If
					End If

					DepatrmentNode.ForeColor = Color.DarkGreen

					Dim otdelComps As IEnumerable(Of IComp) = comps _
						.Where(Function(c) c.FILIAL = filialName And c.MESTO = otdelName And c.kabn = String.Empty)
					cFil = otdelComps.Count()

					If cFil <> 0 Then
						Call FILING_FILIAL(otdelComps)
					End If

					'Кабинеты Третий уровень дерева
					Dim cabinets As List(Of ICabinet) = _
						frmMain.cabinetService.GetAllByFilialAndOtdel(filial.ID, otdel.ID, sVISIBLE = 1)

					If cabinets.Count > 0 Then
						For Each cabinet As ICabinet In cabinets
							cabinetName = cabinet.Name

							Dim OfficeNode As New TreeNode(cabinet.Name, 2, 2)
							OfficeNode.Tag = "K|" & cabinet.ID
							sTEN = "K|" & cabinet.ID
							DepatrmentNode.Nodes.Add(OfficeNode)

							BrancheNode1 = OfficeNode

							If KCKey = 0 And Len(DCKey) <> 0 Then
								If DCKey = sTEN Then
									SelectedNode = OfficeNode
									SelectedNode.Expand()
								End If
							End If

							OfficeNode.ForeColor = Color.DarkGoldenrod

							Dim cabinetComps As IEnumerable(Of IComp) = comps _
								.Where(Function(c) c.FILIAL = filialName And c.MESTO = otdelName And c.kabn = cabinetName)
							cFil = cabinetComps.Count()

							If cFil <> 0 Then
								Call FILING_FILIAL(cabinetComps)
							End If
						Next
					End If
					'Конец кабинетов
				End If
			Next
		Next

		lstgroups.Nodes.Add(nodeRoot)

		If KCKey = 0 And Len(DCKey) = 0 Then
			lstgroups.SelectedNode = nodeRoot
			lstgroups.Nodes(0).Expand()
		ElseIf Not IsNothing(selectedNode) Then
			lstgroups.SelectedNode = SelectedNode
			lstgroups.SelectedNode.Expand()
		End If

		'frmComputers.OneStart = 1

		' MessageBox.Show("done! " & Math.Round(Now.Subtract(starttime).TotalSeconds, 4).ToString)

		'lstgroups.visible = True
exitsub:
		Exit Sub
ERR1:
		'lstgroups.visible = True
		'MsgBox Err.Description

		Select Case Err.Number
			Case 3021 'ignore, no entries in list
				'MsgBox(Err.Description, vbCritical, ProGramName)
				Resume Next
			Case Else
				MsgBox(Err.Description, vbCritical, ProGramName)
		End Select
	End Sub

	Private Sub FILING_TREE(ByVal lstgroups As TreeView, ByVal iTipTehn As String, ByVal TipPC As String, ByVal NET_NAME As String, ByVal PSEVDONIM As String, ByVal iD As String, ByVal Spisan As String, ByVal DepNode As TreeNode, ByVal OS As String, ByVal n_set As String, ByVal balans As String)
		Dim iC As String
		Dim iA As String
		Dim iB As String
		Dim sTREENAME As String
		Dim N_NAME As String
		Dim P_NAME As String
		Dim L_NAME As String
		Dim iPSid As Long

		Dim uNameS As String

		Dim objIniFile As New IniFile(PrPath & "base.ini")
		KCKey = objIniFile.GetString("general", "DK", 0)
		DCKey = objIniFile.GetString("general", "Default", 0)
		sTREENAME = objIniFile.GetString("general", "NETNAME", "1")

		If iTipTehn = "PC" Then
			iC = TipPC
		Else
			iC = "Рабочая станция"
		End If

		If iC = "Ноутбук" Or iC = "notebook" Or iC = "Notebook" Or iC = "NoteBook" Then
			iC = "Ноутбук"
		End If

		If iC = "КПК" Or iC = "Pocket PC" Or iC = "Pocket" Or iC = "Palm" Then
			iC = "КПК"
		End If

		If iC = "Сервер" Or iC = "Server" Or iC = "Сервер для тонких клиентов" Or iC = "Сервер видео наблюдения" Then
			iC = "Сервер"
		End If

		'Pocket PC

		Select Case iC
			Case "Рабочая станция"
				iA = 4
				iB = 4
			Case "Сервер"
				iA = 3
				iB = 3
			Case "КПК"
				iA = 31
				iB = 31
			Case "Ноутбук"
				iA = 5
				iB = 5
			Case Else
				iA = 4
				iB = 4
		End Select

		'Определяем подчиненное оборудование (в составе)
		Dim d() As String

		If iTipTehn = "MONITOR" Then
			If Len(OS) > 0 Then
				d = Split(OS, "№")
			End If
		End If

		'Dim TEHNodePS As TreeNode

		Select Case sTREENAME
			Case 0
				N_NAME = NET_NAME
				P_NAME = PSEVDONIM

				If Len(N_NAME) = 0 Then
					N_NAME = "NoName"
				End If

				If Len(P_NAME) = 0 Then
					P_NAME = "NoName"
				End If

				If N_NAME = P_NAME Then
					L_NAME = N_NAME
				Else
					L_NAME = N_NAME & " (" & P_NAME & ")"
				End If
			Case 2
				P_NAME = PSEVDONIM

				If Len(P_NAME) = 0 Then
					P_NAME = "NoName"
				End If
				L_NAME = P_NAME
			Case 1
				N_NAME = NET_NAME
				If Len(N_NAME) = 0 Then
					N_NAME = "NoName"
				End If

				L_NAME = N_NAME
		End Select

		'My.Application.DoEvents()

		Select Case iTipTehn
			Case "CNT"
				Dim uname As String

				On Error Resume Next

				If Len(NET_NAME) = 0 Then
					uname = ""
				Else
					Dim rsOT As ADODB.Recordset
					rsOT = New ADODB.Recordset
					rsOT.Open("SELECT A FROM spr_other where Name ='" & NET_NAME & "'", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

					With rsOT
						If Not IsDBNull(.Fields("A").Value) Then uname = .Fields("A").Value
					End With
					rsOT.Close()
					rsOT = Nothing
				End If

				Dim TEHNodeCNT As New TreeNode(L_NAME, uname, uname)
				TEHNodeCNT.Tag = "C|" & iD
				DepNode.Nodes.Add(TEHNodeCNT)

				If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
					TEHNodeCNT.ForeColor = Color.DimGray
					TEHNodeCNT.NodeFont = New Font(lstgroups.Font, 8)
				End If

				If balans = "1" Or balans = "True" Or balans = "-1" Then
					If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
						TEHNodeCNT.NodeFont = New Font(lstgroups.Font, 10)
					Else
						TEHNodeCNT.NodeFont = New Font(lstgroups.Font, 2)
					End If
				End If

				If KCKey <> 0 Then
					If KCKey = iD Then
						SelectedNode = TEHNodeCNT
						SelectedNode.Expand()
					End If
				End If

				'#####################################################################
				'#####################################################################
				'#                          КОНТЕЙНЕР
				'#####################################################################
				'#####################################################################

				Dim sText As String = objIniFile.GetString("general", "Tree_S", 0)

				Dim pcls As IEnumerable(Of IComp) = frmMain.compService _
					.GetAll() _
					.Where(Function(c) c.PCL = iD)

				If pcls.Count() > 0 Then
					Select Case sText
						Case 0
							pcls = pcls.OrderBy(Function(c) c.PSEVDONIM).OrderBy(Function(c) c.TipTehn)
						Case 1
							pcls = pcls.OrderBy(Function(c) c.TipTehn).OrderBy(Function(c) c.PSEVDONIM)
					End Select

					For Each pcl As IComp In pcls
						Spisan = pcl.Spisan
						balans = pcl.Balans

						Select Case sTREENAME
							Case 0
								N_NAME = pcl.NET_NAME
								P_NAME = pcl.PSEVDONIM

								If Len(N_NAME) = 0 Then
									N_NAME = "NoName"
								End If

								If Len(P_NAME) = 0 Then
									P_NAME = "NoName"
								End If
								L_NAME = N_NAME & " (" & P_NAME & ")"
							Case 2
								P_NAME = pcl.PSEVDONIM

								If Len(P_NAME) = 0 Then
									P_NAME = "NoName"
								End If
								L_NAME = P_NAME
							Case 1
								N_NAME = pcl.NET_NAME
								If Len(N_NAME) = 0 Then
									N_NAME = "NoName"
								End If

								L_NAME = N_NAME
						End Select

						Select Case pcl.TipTehn
							Case "NET"
								Dim TEHNodePC As New TreeNode(L_NAME, 10, 10)
								TEHNodePC.Tag = "C|" & pcl.ID
								TEHNodeCNT.Nodes.Add(TEHNodePC)
								iD = pcl.ID

								Select Case n_set
									Case "Off"
										TEHNodePC.ForeColor = Color.Red
									Case "Defective"
										TEHNodePC.ForeColor = Color.Blue
									Case Else
										TEHNodePC.ForeColor = Color.Green
								End Select

								If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
									TEHNodePC.ForeColor = Color.DimGray
									TEHNodePC.NodeFont = New Font(lstgroups.Font, 8)
								End If

								If balans = "1" Or balans = "True" Or balans = "-1" Then
									If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
										TEHNodePC.NodeFont = New Font(lstgroups.Font, 10)
									Else
										TEHNodePC.NodeFont = New Font(lstgroups.Font, 2)
									End If
								End If

								If KCKey <> 0 Then
									If KCKey = iD Then
										SelectedNode = TEHNodePC
										SelectedNode.Expand()
									End If
								End If
							Case "PC"
								iC = pcl.TIP_COMPA

								If iC = "Ноутбук" Or iC = "notebook" Or iC = "Notebook" Or iC = "NoteBook" Then
									iC = "Ноутбук"
								End If

								If iC = "КПК" Or iC = "Pocket PC" Or iC = "Pocket" Or iC = "Palm" Then
									iC = "КПК"
								End If

								If iC = "Сервер" Or iC = "Server" Or iC = "Сервер для тонких клиентов" Or iC = "Сервер видео наблюдения" Then
									iC = "Сервер"
								End If

								'Pocket PC

								Select Case iC
									Case "Рабочая станция"
										iA = 4
									Case "Сервер"
										iA = 3
									Case "КПК"
										iA = 31
									Case "Ноутбук"
										iA = 5
									Case Else
										iA = 4
								End Select

								Dim TEHNodePC As New TreeNode(L_NAME, iA, iA)
								iD = pcl.ID
								TEHNodePC.Tag = "C|" & pcl.ID
								iPSid = pcl.ID

								TEHNodeCNT.Nodes.Add(TEHNodePC)

								'TEHNode.ForeColor = Color.Blue

								If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
									TEHNodePC.ForeColor = Color.DimGray
									TEHNodePC.NodeFont = New Font(lstgroups.Font, 8)
								End If

								If balans = "1" Or balans = "True" Or balans = "-1" Then
									If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
										TEHNodePC.NodeFont = New Font(lstgroups.Font, 10)
									Else
										TEHNodePC.NodeFont = New Font(lstgroups.Font, 2)
									End If
								End If

								If KCKey <> 0 Then
									If KCKey = pcl.ID Then
										SelectedNode = TEHNodePC
										SelectedNode.Expand()
									End If
								End If

								'#####################################################################
								'#####################################################################
								'#                          Компьютер в контейнере
								'#####################################################################
								'#####################################################################

								Dim pcls_inner As IEnumerable(Of IComp) = frmMain.compService _
									.GetAll() _
									.Where(Function(c) c.PCL = iD)

								If pcls_inner.Count() > 0 Then
									Select Case sText
										Case 0
											pcls_inner = pcls_inner.OrderBy(Function(c) c.PSEVDONIM).OrderBy(Function(c) c.TipTehn)
										Case 1
											pcls_inner = pcls_inner.OrderBy(Function(c) c.TipTehn).OrderBy(Function(c) c.PSEVDONIM)
									End Select

									For Each inner As IComp In pcls_inner
										Spisan = inner.Spisan
										balans = inner.Balans

										Select Case sTREENAME
											Case 0
												N_NAME = inner.NET_NAME
												P_NAME = inner.PSEVDONIM

												If Len(N_NAME) = 0 Then
													N_NAME = "NoName"
												End If

												If Len(P_NAME) = 0 Then
													P_NAME = "NoName"
												End If

												If N_NAME = P_NAME Then
													L_NAME = N_NAME
												Else
													L_NAME = N_NAME & " (" & P_NAME & ")"
												End If
											Case 2
												P_NAME = inner.PSEVDONIM

												If Len(P_NAME) = 0 Then
													P_NAME = "NoName"
												End If
												L_NAME = P_NAME
											Case 1
												N_NAME = inner.NET_NAME
												If Len(N_NAME) = 0 Then
													N_NAME = "NoName"
												End If

												L_NAME = N_NAME
										End Select

										Select Case inner.TipTehn
											Case "Printer"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 7)
											Case "MFU"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 8)
											Case "SCANER"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 14)
											Case "ZIP"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 15)
											Case "PHONE"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 12)
											Case "OT"
												' Dim uname As String

												On Error Resume Next

												If Len(inner.TIP_COMPA) = 0 Then
													uname = ""
												Else
													Dim rsOT As ADODB.Recordset
													rsOT = New ADODB.Recordset
													rsOT.Open("SELECT A FROM spr_other where Name ='" & inner.TIP_COMPA & "'", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

													With rsOT
														If Not IsDBNull(.Fields("A").Value) Then uname = .Fields("A").Value
													End With
													rsOT.Close()
													rsOT = Nothing
												End If

												If Len(uname) = 0 Or uname = " " Or uname = " 0" Or uname = "" Then
													iA = 16
												Else
													iA = uname
												End If

												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, iA)
											Case "MONITOR"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 17)
												'--------------VIP_Graff Добавление новой перефирии Начало-----------------
											Case "USB"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 18)
											Case "SOUND"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 44)
											Case "IBP"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 41)
											Case "FS"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 61)
											Case "KEYB"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 46)
											Case "MOUSE"
												Filling_TREE_DATA(lstgroups, inner.ID, TEHNodePC, Spisan, balans, L_NAME, 47)
												'--------------VIP_Graff Добавление новой перефирии Конец------------------
											Case Else
										End Select
									Next
								End If

							'#####################################################################
							'#####################################################################
							'#                          Техника в контейнере
							'#####################################################################
							'#####################################################################
							Case "Printer"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 7)
							Case "MFU"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 8)
							Case "SCANER"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 14)
							Case "ZIP"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 15)
							Case "PHONE"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 12)
							Case "OT"
								'Dim uname As String

								On Error Resume Next

								If Len(pcl.TIP_COMPA) = 0 Then
									uname = ""
								Else
									Dim rsOT As ADODB.Recordset
									rsOT = New ADODB.Recordset
									rsOT.Open("SELECT A FROM spr_other where Name ='" & pcl.TIP_COMPA & "'", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

									With rsOT
										If Not IsDBNull(.Fields("A").Value) Then uname = .Fields("A").Value
									End With
									rsOT.Close()
									rsOT = Nothing
								End If

								If Len(uname) = 0 Or uname = " " Or uname = " 0" Or uname = "" Then
									iA = 16
								Else
									iA = uname
								End If

								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, iA)
							Case "MONITOR"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 17)
								'--------------VIP_Graff Добавление новой перефирии Начало-----------------
							Case "USB"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 18)
							Case "SOUND"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 44)
							Case "IBP"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 41)
							Case "FS"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 61)
							Case "KEYB"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 46)
							Case "MOUSE"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodeCNT, Spisan, balans, L_NAME, 47)
								'--------------VIP_Graff Добавление новой перефирии Конец------------------
							Case Else
						End Select
					Next
				End If

				'#####################################################################
				'#####################################################################
				'#                          Компьютер
				'#####################################################################
				'#####################################################################
			Case "PC"
				Dim TEHNodePC As New TreeNode(L_NAME, iA, iB)

				TEHNodePC.Tag = "C|" & iD
				iPSid = iD

				DepNode.Nodes.Add(TEHNodePC)

				'TEHNode.ForeColor = Color.Blue
				If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
					TEHNodePC.ForeColor = Color.DimGray
					TEHNodePC.NodeFont = New Font(lstgroups.Font, 8)
				End If

				If balans = "1" Or balans = "True" Or balans = "-1" Then
					If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
						TEHNodePC.NodeFont = New Font(lstgroups.Font, 10)
					Else
						TEHNodePC.NodeFont = New Font(lstgroups.Font, 2)
					End If
				End If

				If KCKey <> 0 Then
					If KCKey = iD Then
						SelectedNode = TEHNodePC
						SelectedNode.Expand()
					End If
				End If

				'########################################################################
				'########################################################################
				'########################################################################

				Dim sText As String = objIniFile.GetString("general", "Tree_S", 0)

				Dim pcls As IEnumerable(Of IComp) = frmMain.compService _
					.GetAll() _
					.Where(Function(c) c.PCL = iD)

				If pcls.Count() > 0 Then
					Select Case sText
						Case 0
							pcls = pcls.OrderBy(Function(c) c.PSEVDONIM).OrderBy(Function(c) c.TipTehn)
						Case 1
							pcls = pcls.OrderBy(Function(c) c.TipTehn).OrderBy(Function(c) c.PSEVDONIM)
					End Select

					For Each pcl As IComp In pcls
						Spisan = pcl.Spisan
						balans = pcl.Balans

						Select Case sTREENAME
							Case 0
								N_NAME = pcl.NET_NAME
								P_NAME = pcl.PSEVDONIM

								If Len(N_NAME) = 0 Then
									N_NAME = "NoName"
								End If

								If Len(P_NAME) = 0 Then
									P_NAME = "NoName"
								End If

								If N_NAME = P_NAME Then
									L_NAME = N_NAME
								Else
									L_NAME = N_NAME & " (" & P_NAME & ")"
								End If
							Case 2
								P_NAME = pcl.PSEVDONIM

								If Len(P_NAME) = 0 Then
									P_NAME = "NoName"
								End If
								L_NAME = P_NAME
							Case 1
								N_NAME = pcl.NET_NAME
								If Len(N_NAME) = 0 Then
									N_NAME = "NoName"
								End If

								L_NAME = N_NAME
						End Select

						Select Case pcl.TipTehn
							Case "Printer"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 7)
							Case "MFU"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 8)
							Case "SCANER"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 14)
							Case "ZIP"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 15)
							Case "PHONE"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 12)
							Case "OT"
								Dim uname As String

								On Error Resume Next

								If Len(pcl.TIP_COMPA) = 0 Then
									uname = ""
								Else
									Dim rsOT As ADODB.Recordset
									rsOT = New ADODB.Recordset
									rsOT.Open("SELECT A FROM spr_other where Name ='" & pcl.TIP_COMPA & "'", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

									With rsOT
										If Not IsDBNull(.Fields("A").Value) Then uname = .Fields("A").Value
									End With
									rsOT.Close()
									rsOT = Nothing
								End If

								If Len(uname) = 0 Or uname = " " Or uname = " 0" Or uname = "" Then
									iA = 16
								Else
									iA = uname
								End If

								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, iA)
							Case "MONITOR"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 17)
								'--------------VIP_Graff Добавление новой перефирии Начало-----------------
							Case "USB"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 18)
							Case "SOUND"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 44)
							Case "IBP"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 41)
							Case "FS"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 61)
							Case "KEYB"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 46)
							Case "MOUSE"
								Filling_TREE_DATA(lstgroups, pcl.ID, TEHNodePC, Spisan, balans, L_NAME, 47)
								'--------------VIP_Graff Добавление новой перефирии Конец------------------
							Case Else
						End Select
					Next
				End If
			Case "Printer"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 7)
			Case "MFU"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 8)
			Case "KOpir"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 9)
			Case "NET"
				Dim TEHNode As New TreeNode(L_NAME, 10, 10)
				TEHNode.Tag = "C|" & iD
				DepNode.Nodes.Add(TEHNode)

				Select Case n_set
					Case "Off"
						TEHNode.ForeColor = Color.Red
					Case "Defective"
						TEHNode.ForeColor = Color.Blue
					Case Else
						TEHNode.ForeColor = Color.Green
				End Select

				If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
					TEHNode.ForeColor = Color.DimGray
					TEHNode.NodeFont = New Font(lstgroups.Font, 8)
				End If

				If balans = "1" Or balans = "True" Or balans = "-1" Then
					If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
						TEHNode.NodeFont = New Font(lstgroups.Font, 10)
					Else
						TEHNode.NodeFont = New Font(lstgroups.Font, 2)
					End If
				End If

				If KCKey <> 0 Then
					If KCKey = iD Then
						SelectedNode = TEHNode
						SelectedNode.Expand()
					End If
				End If
			Case "PHOTO"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 11)
			Case "PHONE"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 12)
			Case "FAX"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 13)
			Case "SCANER"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 14)
			Case "ZIP"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 15)
			Case "OT"
				Dim uname As String

				On Error Resume Next

				If Len(TipPC) = 0 Then
					uname = ""
				Else
					Dim rsOT As ADODB.Recordset
					rsOT = New ADODB.Recordset
					rsOT.Open("SELECT A FROM spr_other where Name ='" & TipPC & "'", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

					With rsOT
						If Not IsDBNull(.Fields("A").Value) Then uname = .Fields("A").Value
					End With
					rsOT.Close()
					rsOT = Nothing
				End If

				If Len(uname) = 0 Or uname = " " Or uname = " 0" Or uname = "" Then
					iA = 16
				Else
					iA = uname
				End If

				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, iA)
			Case "MONITOR"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 17)
				'--------------VIP_Graff Добавление новой перефирии Начало-----------------
			Case "USB"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 18)
			Case "SOUND"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 44)
			Case "IBP"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 41)
			Case "FS"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 61)
			Case "KEYB"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 46)
			Case "MOUSE"
				Filling_TREE_DATA(lstgroups, iD, DepNode, Spisan, balans, L_NAME, 47)
				'--------------VIP_Graff Добавление новой перефирии Конец------------------
			Case Else
		End Select
	End Sub

	Public Sub Filling_TREE_DATA(ByVal lstgroups As TreeView, ByVal sID As Integer, ByVal TEHNodePCL As TreeNode, ByVal Spisan As String, ByVal balans As String, ByVal L_NAME As String, ByVal sNUM As Decimal)
		Dim TEHNodeCNT As New TreeNode(L_NAME, sNUM, sNUM)
		TEHNodeCNT.Tag = "C|" & sID
		TEHNodePCL.Nodes.Add(TEHNodeCNT)

		If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
			TEHNodeCNT.ForeColor = Color.DimGray
			TEHNodeCNT.NodeFont = New Font(lstgroups.Font, 8)
		End If

		If balans = "1" Or balans = "True" Or balans = "-1" Then
			If Spisan = "1" Or Spisan = "True" Or Spisan = "-1" Then
				TEHNodeCNT.NodeFont = New Font(lstgroups.Font, 10)
			Else
				TEHNodeCNT.NodeFont = New Font(lstgroups.Font, 2)
			End If
		End If

		If KCKey <> 0 Then
			If KCKey = sID Then
				SelectedNode = TEHNodeCNT
				SelectedNode.Expand()
			End If
		End If
	End Sub
End Module
